/**
 * Copyright (c) 2009 blurredVision
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
 * associated documentation files (the "Software"), to deal in the Software without restriction, 
 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 
 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all copies or 
 * substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 
 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT 
 * OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
#include "StdAfx.h"
#include "../guidefaults.h"
#include "../MenuItem.h"
#include "../Menu.h"

namespace gui {

MenuItem::MenuItem(const std::string &title)
:	m_sTitle(title),
	m_pList(NULL),
	m_pMenu(NULL)
{
	vDim.y = font->getSize();
	vDim.x = static_cast<int>(m_sTitle.size()) * font->getSize();
	m_pList = new List();
	m_pList->setSelectableEnabled(false);
	m_pList->setHighlightable(true);
	m_pList->setVisible(false);
	m_pList->addMouseListener(this);
	this->add(m_pList);
}

MenuItem::~MenuItem(void)
{
}

const std::string& MenuItem::getTitle() const
{
	return m_sTitle;
}

void MenuItem::draw()
{
	if (pParent && pParent->getGUIType() == GUI_MENUITEM) 
		pRenderer->drawFilledRect(vPos.x, vPos.y, vDim.x, vDim.y, vBackColor);

	font->print(m_sTitle, vPos.x+vOffset.x, vPos.y+vOffset.y, 0, FONT_LEFT, 0, 0, vTextColor);

	m_pList->draw();
	//Component::drawElements();
}

void MenuItem::addItem(MenuItem *item)
{
	// Add this item as a component
	Component::add(item);
	item->setVisible(false);
	// Add the string of the item to the title
	m_pList->addString( item->getTitle() );
}

bool MenuItem::processMouse(int _x, int _y, int _z, int _button)
{
	if (Component::processMouse(_x, _y, _z, _button)) {
		if (m_pList->isVisible()) {
			// The first element if the List component
			int idx = m_pList->getHighlightedIndex();
			if (idx != -1) {
				MenuItem *mi = (MenuItem*) components.at(idx+1);
				if (mi->m_pList->getNumItems() > 0) {
					mi->open();
				}
			}
		}
		return true;
	}

	return false;
}

void MenuItem::onMousePressed(const MouseEvent &me)
{
	Component::onMousePressed(me);
	Component::performAction(me.x, me.y, me.button);
	int yPos = vPos.y + iRowSize;
	m_pList->setPosition(0, yPos);
	open();
}

void MenuItem::onMousePressEvent(const MouseEvent &mevent)
{
	close();
}

void MenuItem::onMouseLeaveEvent(const MouseEvent &mevent)
{
	close();
}

void MenuItem::close()
{
	if (m_pMenu)
		m_pMenu->setOpenItem(NULL);
	m_pList->setVisible(false);
}
void MenuItem::open()
{
	if (m_pMenu) 
		m_pMenu->setOpenItem(this);
	m_pList->setVisible(true);
}


} // namespace
